package cn.edu360.lucene;

import org.apache.lucene.document.*;
import org.apache.lucene.document.Field.Store;

public class Book {

    private Long id;

    private String title;

    private String author;

    private String content;

    private Double price;

    private String url;

    public Book() {
    }

    public Book(Long id, String title, String author, String content, Double price, String url) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.content = content;
        this.price = price;
        this.url = url;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Document toDocument() {
        //Lucene存储的格式（Map装的k,v）
        Document doc = new Document();
        //向文档中添加一个long类型的属性，建立索引
        doc.add(new LongPoint("id", id));
        //在文档中存储
        doc.add(new StoredField("id", id));


        //设置一个文本类型，会对内容进行分词，建立索引，并将内容在文档中存储
        doc.add(new TextField("title", title, Store.YES));
        //设置一个文本类型，会对内容进行分词，建立索引，存在文档中存储 / No代表不存储
        doc.add(new TextField("content", content, Store.YES));

        //StringField，不分词，建立索引，文档中存储
        doc.add(new StringField("author", author, Store.YES));

        doc.add(new DoublePoint("price",getPrice()));
        doc.add(new StoredField("price", getPrice()));


        //不分词，不建立索引，在文档中存储，
        doc.add(new StoredField("url", url));
        return doc;
    }

    public static Book parseArticle(Document doc) {
        Long id = Long.parseLong(doc.get("id"));
        String title = doc.get("title");
        String content = doc.get("content");
        String author = doc.get("author");
        String url = doc.get("url");
        Double price = Double.parseDouble(doc.get("price"));
        Book book = new Book(id, title,author, content,  price, url);
        return book;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", content='" + content + '\'' +
                ", price=" + price +
                ", url='" + url + '\'' +
                '}';
    }



    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}
